package team.seekers.hula.course.resource.sectionTalk.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.course.resource.sectionContent.dao.SectionContentDao;
import team.seekers.hula.course.resource.sectionContent.entity.dao.SectionContentDO;
import team.seekers.hula.course.resource.sectionContentComment.entity.dao.SectionContentCommentDO;
import team.seekers.hula.course.resource.sectionTalk.entity.dao.SectionTalkDO;
import team.seekers.hula.course.resource.sectionTalk.entity.dto.SectionTalkQueryDTO;
import team.seekers.hula.course.resource.sectionTalk.entity.vo.SectionTalkListManageVO;
import team.seekers.hula.course.resource.sectionTalk.mapper.SectionTalkMapper;
import team.seekers.hula.course.structure.section.dao.SectionDao;
import team.seekers.hula.course.structure.section.entity.dao.SectionDO;
import team.seekers.hula.course.structure.section.service.SectionService;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class SectionTalkManageService extends ServiceImpl<SectionTalkMapper, SectionTalkDO> {

    @Autowired
    @Lazy
    private SectionService sectionService;

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    private SectionDao sectionDao;

    public PageResult<SectionTalkListManageVO> page(SectionTalkQueryDTO queryDTO) {
        // 用section名称筛选
        LambdaQueryChainWrapper<SectionDO> sectionContentQuery = sectionDao.lambdaQuery();
        sectionContentQuery.eq(SectionDO::getCourseId,queryDTO.getCourseId());
        if (!StringUtils.isEmpty(queryDTO.getSectionName())){
            sectionContentQuery.like(SectionDO::getName,queryDTO.getSectionName());
        }
        List<String> sectionId = sectionContentQuery
                .select(SectionDO::getId).list()
                .stream()
                .map(SectionDO::getId)
                .collect(Collectors.toList());

        if(sectionId.size() == 0){
            return new PageResult<>();
        }


        LambdaQueryChainWrapper<SectionTalkDO> lambdaQuery = lambdaQuery();
        lambdaQuery.in(SectionTalkDO::getSectionId,sectionId);
        if(!StringUtils.isEmpty(queryDTO.getBegin())){
            lambdaQuery.ge(SectionTalkDO::getGmtCreate,queryDTO.getBegin());
        }
        if(!StringUtils.isEmpty(queryDTO.getEnd())){
            lambdaQuery.le(SectionTalkDO::getGmtCreate,queryDTO.getEnd());
        }

        if(!StringUtils.isEmpty(queryDTO.getNickname())){
            List<String> userIds =  userService.listIdsLikeNickname(queryDTO.getNickname());
            if(userIds != null && userIds.size() > 0){
                lambdaQuery.in(SectionTalkDO::getUserId,userIds);
            }else {
                return new PageResult<>();
            }
        }
        if(!StringUtils.isEmpty(queryDTO.getContent())){
            lambdaQuery.like(SectionTalkDO::getContent,queryDTO.getContent());
        }
        if(!StringUtils.isEmpty(queryDTO.getId())){
            lambdaQuery.eq(SectionTalkDO::getId,queryDTO.getId());
        }
        //lambdaQuery.orderByDesc(SectionTalkDO::getGood);

        Page<SectionTalkDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<SectionTalkDO> records = page.getRecords();
        if(records == null){
            return null;
        }
        List<SectionTalkListManageVO> list = new ArrayList<>();
        for (SectionTalkDO record : records) {
            SectionTalkListManageVO sectionTalkListManageVO = new SectionTalkListManageVO();
            BeanUtils.copyProperties(record,sectionTalkListManageVO);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss");
            String time = simpleDateFormat.format(record.getGmtCreate());
            sectionTalkListManageVO.setGmtCreate(time);

            sectionTalkListManageVO.setSectionName(sectionService.getNameById(record.getSectionId()));
            if(record.getVisibility()==0){
                sectionTalkListManageVO.setVisibilityText("仅自己可见");
            }else if(record.getVisibility()==1){
                sectionTalkListManageVO.setVisibilityText("仅关注可见");
            }else if(record.getVisibility()==2){
                sectionTalkListManageVO.setVisibilityText("全部可见");
            }else{
                sectionTalkListManageVO.setVisibilityText("未知权限");
            }

            sectionTalkListManageVO.setUserInfo(userService.getBasicInfoVO(record.getUserId()));
            list.add(sectionTalkListManageVO);
        }
        return new PageResult<>(total,list);
    }

    public void updata(String id,Integer visibility){
        SectionTalkDO sectionTalkDO = getById(id);
        sectionTalkDO.setVisibility(visibility);
        updateById(sectionTalkDO);
    }
}
